<?php
tsLoad('/core/CoreLib/IPage.class.php');

abstract class AbstractMysqlPage implements IPage
{
    
    /**
     * 总记录数
     */
    private $totalRows;
    
    /**
     * 每页多少记录
     */
    private $pageRows;
    
    /**
     * 总页数
     */
    private $totalPages;
    
    /**
     * 当前页
     */
    private $curPageNo;
    
    /**
     * 是否第一次查询
     */
    private $initFlag;
    
    /**
     * 绑定分页URL地址
     */
    private $pageUrl;
    
    /**
     * 排序字段名
     */
    protected $sortCol;
    
    /**
     * 排序类型
     */
    protected $sortType;
    
    /**
     * 功能：默认每页记录数
     * @return
     */
    public function __construct() {
        $this->pageRows = 30;
        $this->curPageNo = 1;
        $this->initFlag = false;
    }
    
    public function init() {
        $this->totalRows = $this->calTotalRows();
        $this->totalPages = $this->calTotalPages();
        $this->initFlag = true;
    }
    
    /**
     * 功能:取得记录数
     */
    public function getTotalRows() {
        if (!$this->initFlag)
            $this->init();
        return $this->totalRows;
    }
    
    public function getPageRows() {
        return $his->pageRows;
    }
    
    public function setPageRows($pageRows) {
        if ($pageRows < 1)
            $this->pageRows = 1;
        else
            $this->pageRows = $pageRows;
        if (!$this->initFlag)
            $this->init();
        else
            $this->totalPages = $this->calTotalPages();
    }
    
    /**
     * 功能:取得总页面数
     */
    public function getTotalPages() {
        if (!$this->initFlag)
            $this->init();
        return $this->totalPages;
    }
    
    public function getCurPageNo() {
        return $this->curPageNo;
    }
    
    public function setCurPageNo($curPageNo) {
        if (!$this->initFlag)
            $this->init();
        if ($curPageNo > $this->totalPages)
            $this->curPageNo = $this->totalPages;
        else
            $this->curPageNo = $curPageNo;
        if ($this->curPageNo < 1)
            $this->curPageNo = 1;
    }
    
    /**
     * 功能：计算总的页数
     *
     * @return
     */
    private function calTotalPages() {        
        $allPages = floor($this->totalRows / $this->pageRows);
        if ($this->totalRows % $this->pageRows > 0)
            $allPages++;
        return $allPages;
    }
    
    /**
     * 功能：取得数据集
     */
    public function getCurPageData() {
        if (!$this->initFlag)
            $this->init();
       $sRowNo = $this->pageRows * ($this->curPageNo - 1);
    
        return $this->getList($sRowNo,$this->pageRows);
    }
    
    /**
     * 功能:计算总记录数
     *
     * @return
     */
    protected abstract function calTotalRows();
    
    /**
     * 装配pageInfo到前台供pageInfo模块使用
     * @param pageInfo 待装配类
    */
    public function assemblePageInfo(){ 
        $pageInfo = array();
        //装配总页数
        $pageInfo['countPage'] = $this->totalPages;
        //装配总记录数
        $pageInfo['countRow'] = $this->totalRows;
        //装配当前页
        $pageInfo['currentPage'] = $this->curPageNo;
        //每页记录数
        $pageInfo['pageRows'] = $this->pageRows;
        //装备待查询url
        $pageInfo['pageUrl'] = $this->pageUrl;
        
        return $pageInfo;
    }
    
    /**
     * 功能:取得数据集
     *
     * @param sRowNo 开始序号
     * @param coreSql 核心SQL部分
     * @return
     */
    protected abstract function getList($sRowNo,$pageRows);
    
    public function getPageUrl() {
        return $this->pageUrl;
    }
    
    public function setPageUrl($pageUrl) {
        $this->pageUrl = $pageUrl;
    }
    
    public function getSortCol() {
        return $this->sortCol;
    }
    
    public function setSortCol($sortCol) {
        $this->sortCol = $sortCol;
    }
    
    public function getSortType() {
        return $this->sortType;
    }
    
    public function setSortType($sortType) {
        $this->sortType = $sortType;
    }
    
    public function setTotalRows($totalRows) {
        $this->totalRows = $totalRows;
    }
    
    public function setTotalPages($totalPages) {
        $this->totalPages = $totalPages;
    }
    
    /** SQL参数 */
    public $condition = null;
    
    /**
     * 初始化方法
     * @param map
     * @param pageRows  每页显示行数
     * @param curPageNo 当前行数
     * @return
     */
    public function initPage($condition, $pageRows, $curPageNo) {
        tsLoad('/core/CoreLib/PageInfo.class.php');
        
        $this->condition = $condition;
        $this->setPageRows($pageRows);
        $this->setCurPageNo($curPageNo);
        $pageInfo = $this->assemblePageInfo();
        return $pageInfo;
    }
}

?>